package Configurations;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public final class FileOperations {

    /* Returns the file list of the current directory */
    public static File[] getFiles() {
        File currentDirectory;
        try {
            currentDirectory = new File(new File(".").getCanonicalPath() + "/docroot");
            System.out.println(currentDirectory.getCanonicalPath());
            File fileList[] = currentDirectory.listFiles();
            return fileList;
        } catch (IOException ex) {
            System.out.println("Error: Unable to get file list");
        }
        return null;
    }

    /* Returns whether or not a file with the specified name exists in the main
     * directory of the server.
     */
    public static boolean fileExist(String fileName) {
        boolean exists = false;
        File[] fileList = getFiles();
        for (int i = 0; i < fileList.length; i++) {
            if (fileList[i].getName().equals(fileName)) {
                exists = true;
            }
        }
        return exists;
    }

    /* Extract files from a zip folder
     * Algorithm from http://www.java2s.com/Tutorial/Java/0180__File/Unzip.htm
     */
    public static void unzip(File file) {
        try {
            ZipFile zipFile = new ZipFile(file);         
            Enumeration enume = zipFile.entries();
            while (enume.hasMoreElements()) {
                ZipEntry zipEntry = (ZipEntry) enume.nextElement();
                int startIndex = zipEntry.getName().lastIndexOf("/");
                String saveLocation = "";
                if (startIndex < 0) {
                    startIndex = 0;
                    saveLocation = file.getParent() + "/" + zipEntry.getName().substring(startIndex);
                } else {
                    saveLocation = file.getParent() + zipEntry.getName().substring(startIndex);
                }
                System.out.println(file.getParent() + zipEntry.getName().substring(startIndex));
                //String saveLocation = (new File("")).getAbsolutePath() + NAME_OF_SAVE_FOLDER + zipEntry.getName().substring(zipEntry.getName().lastIndexOf("/"));
                System.out.println("Extracting " + zipEntry.getName());
                System.out.println("Extract to " + saveLocation);
                FileOutputStream fos = new FileOutputStream(saveLocation);
                InputStream is = zipFile.getInputStream(zipEntry);
                for (int i = is.read(); i != -1; i = is.read()) {
                    fos.write(i);
                }
                is.close();
                fos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error: Unable to unzip file");
        }
    }
  
}
